(function (win, doc, objName) {
  // 定义全局对象
  function Obj() { };
  Obj.prototype = {
    /**
     * 简单cookie获取与设置
     * @param  {string} name  cookie设置或获取
     * @param  {string} value 设置cookie的值
     * @param  {string} op 设置cookie的参数
     */
    cookie: function (name, value, op) {
      // 变量获取
      var op = op || {},
        expires = '',
        d = new Date();
      if (typeof value != 'undefined') {
        if (value === null) {
          op.expires = -1
        }; //删除
        if (op.expires && (typeof op.expires == "number" || op.expires.toUTCString)) {
          if (typeof op.expires == "number") {
            d.setTime(d.getTime() + (op.expires * 36e5))
          } // 以小时为单位
          else {
            d = op.expires
          }
          expires = "; expires=" + d.toUTCString();
        }
        doc.cookie = [name, "=", encodeURIComponent(value), expires, op.path ? ("; path=" + op.path) : "", op.domain ? ("; domain=" + op.domain) : "", op.secure ? "; secure" : ""].join('');
      } else {
        return decodeURIComponent(doc.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + name + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null
      }
    },
    /**
     * loadjs 同步或异步方式加载js文件 opt:
     * @param  {[type]} src 加载js地址
     * @param  {[type]} opt  {charset,id,data,async:true} 默认同步输出
     */
    loadjs: function (src, opt) {
      opt = opt || {};
      if (opt.async) {
        var obj = doc.createElement('script');
        obj.src = src;
        obj.async = true;
        obj.type = 'text/javascript';
        if (opt.charset) {
          obj.charset = opt.charset;
        }
        if (opt.data) {
          obj.data = opt.data;
        }
        if (opt.id) {
          obj.id = opt.id;
        }
        (doc.head || doc.getElementsByTagName('head')[0] || doc.docElement).appendChild(obj);
      } else {
        doc.write('<script src="' + src + '"' + (opt.charset ? ' charset="' + opt.charset + '"' : '') + (opt.data ? ' data="' + opt.data + '"' : '') + (opt.id ? ' id="' + opt.id + '"' : '') + '></script>');
      }
    },
    domAnim: function (dis) {
      $(win).scroll(function () {
        var obj = $('.topBannerBox .topBannerBD');
        if ($(win).scrollTop() > dis) {
          obj.attr('class', 'topBannerBD topBannerAnimHidden')
        } else {
          obj.attr('class', 'topBannerBD topBannerAnim')
        }
      })
    },
    getHost: function () {
      var host = location.host.replace(/([a-z]{1,6}\.)?(.+)(\.[a-z]{2,4})(\:\d+)?$/, '$2');
      if (/localhost|127\.0\.0\.1/.test(host) || !host) {
        host = 'kanman';
      }
      return host;
    },
    getScheme: function (data) {
      // 新增加两个schema_Adr
      // 获取全局的漫画信息
      if (data.host === 'zymk') {
        var match = location.pathname.match(/^\/\d+/g);
        var comic_id = (match ? match[0].substr(1) : ''); // 获取到漫画的目录id
        return data.scheme_Adr + '://goto?page=' + (comic_id ? 'dir' : 'index') + '&comic_id=' + comic_id;
        // return [data.scheme_Adr + '://goto?page=' + (comic_id ? 'dir' : 'index') + '&comic_id=' + comic_id];
      } else {
        var gobalInfo = win.gobal_info || {}; // 从文档中获取gobal_info
        var comic_id = gobalInfo.comic_id || '';
        return data.scheme_Adr + '://' + location.hostname.replace('m.', 'www.') + '/open?id=' + (comic_id ? comic_id : '');
        // return [
        //   data.scheme_Adr + '://www.kanman.com/open?id=' + comic_id,
        //   data.scheme_Adr + '://goto?page=' + (comic_id ? 'dir' : 'index') + '&comic_id=' + comic_id
        // ];

      }
    },
    getConfig: function (options) {
      var host = this.getHost(); // 获得host
      var data = options[host];
      data['host'] = host; // 设置host的值
      return $.extend(data, {
        // url: 'http://a.app.qq.com/o/simple.jsp?pkgname=' + data.pkgname + '&ckey=' + data.ckey,
        // iosurl: 'https://itunes.apple.com/app/apple-store/' + data.ios,
        url: 'http://api.yyhao.com/mht_api/v2/getappaddr/?qudao=web_manhuatai',
        iosurl: 'http://api.yyhao.com/mht_api/v2/getappaddr/?qudao=web_manhuatai',
        scheme_Adr: this.getScheme(data)
      });
    },
    createDOM: function (data) {
      // 默认宣传语
      var feature = data.feature || '<span class="jisu">极速</span> <span class="noad">无广告</span> <span class="cache">离线缓存</span>';

      $('body').prepend('<div class="topBannerBox"><div class="topBannerBD topBannerAnim" style="display: block !important;"><span class="close">&times;</span><div class="txtBox"><h2 class="logoTxt">《' + data.appname + '》APP</h2><p class="txtBoxTxt">' + feature + '</p></div><span id="schemeOpen" class="open" onclick="' + objName + '.schemeHandle(this)">打开APP阅读</span></div></div>');
      this.loadjs('http://static.321mh.com/js/clipboard.min.js', { async: true })
      this.createStyle(data);
    },
    createStyle: function (opt) {
      // 默认高度
      var height = opt.height || 100;
      // 默认样式
      var style = opt.style || '.topBannerBox{width:100%;z-index:9999;background:#292929;height:' + height / 2 + 'px;}.topBannerBox .topBannerBD{position:fixed;top:0;width:100%;height:' + height / 2 + 'px;background:rgba(0,0,0,.75);}.topBannerBox .topBannerAnim{-webkit-transform:translateY(0);transform:translateY(0);-webkit-transition:all .5s cubic-bezier(.19,1,.22,1);transition:all .5s cubic-bezier(.19,1,.22,1);z-index:9999;}.topBannerBox .topBannerAnimHidden{-webkit-transform:translateY(-101%);transform:translateY(-101%);-webkit-transition:all .3s cubic-bezier(.55,.055,.675,.19);transition:all .3s cubic-bezier(.55,.055,.675,.19);z-index:9999;}.topBannerBox .close{position:absolute;top:0;right:0;width:24px;height:' + height / 2 + 'px;line-height:' + height / 2 + 'px;color:#999;padding:0;background:none;font-size:24px;font-family:SimSun,Tahoma,arial,SimHei,sans-serif;text-align:center;text-decoration:none;z-index:9999;}[data-dpr="2"] .topBannerBox,[data-dpr="2"] .topBannerBox. topBannerBD{height:' + height + 'px}[data-dpr="3"] .topBannerBox,[data-dpr="3"] .topBannerBox. topBannerBD{height:' + height * 1.5 + 'px}[data-dpr="2"] .topBannerBox .close{width:48px;height:' + height + 'px;line-height:' + height + 'px;}[data-dpr="3"] .topBannerBox .close{width:72px;height:' + height * 1.5 + 'px;;line-height:' + height * 1.5 + 'px;}.topBannerBox,.topBannerBox *,.topBannerBox:after,.topBannerBox:after,.topBannerBox:before,.topBannerBox:before{box-sizing:border-box}.topBannerBox .txtBox,.topBannerBox .open{position:absolute;top:50%;-webkit-transform:translateY(-50%);transform:translateY(-50%)}.topBannerBox .open{right:32px;display:inline-block;width:88px;line-height:30px;border-radius:3px;font-size:12px;text-decoration:none;color:#fff;text-align:center;white-space:nowrap;background:#ff424b}.topBannerBox .txtBox{left:5px}.topBannerBox .txtBoxTxt{color:#fff;font-size:12px; margin:0 0 0 8px;}.topBannerBox .logoTxt{margin:0 0 2px 0;color:#f9f978;font-size:20px;line-height:1;font-weight:400}.topBannerBox .jisu{color:#a7f96f}.topBannerBox .noad{color:#ffcec5}.topBannerBox .cache{color:#9af5bf}@media screen and (max-width:320px){.topBannerBox .logoTxt{font-size:16px}}@media screen and (max-width:375px){.topBannerBox .logoTxt{font-size:16px}}';

      $('head').append('<style>' + style + '</style>');
    },
    removeDOM: function (expires) {
      var _this = this;
      $('.topBannerBox .close').click(function (event) {
        event.stopPropagation();
        event.preventDefault();
        _this.cookie('topBanner2', true, expires ? {
          expires: expires
        } : {
            expires: 0.1
          }); // 默认过期时间为1小时
        $('.topBannerBox').remove(); // 删除节点
      })
    },
    autoOpenScheme: function () {
      var _this = this;
      var config = _this.config;
      var UA = navigator.userAgent.toLowerCase();
      if (UA.match(/(zymk|kanmanhua|manhuatai|kmh_|kmh_|mht_|mht_|zymk_|zymk_);?/i)) {
        // 更改andriod兼容两种方式打开
        var schemeAdr = config.scheme_Adr;
        for (var i = 0; i < schemeAdr.length; i++) {
          _this.creatIframe(schemeAdr[i]); // android、ios都能通过创建iframe自动唤起APP,Scheme
        }
      }
    },
    schemeHandle: function (e) {
      var _this = this;
      var config = _this.config;
      var UA = navigator.userAgent.toLowerCase();
      var schemeAdr = config.scheme_Adr;

      // 添加剪切板拷贝功能
      e.setAttribute('data-schemes', schemeAdr)
      new Clipboard('#schemeOpen', {
        text: function (trigger) {
          return trigger.getAttribute('data-schemes')
        }
      })
      // 添加剪切板拷贝功能结束

      if (UA.match(/(iPhone|iPod|iPad);?/i)) {
        //抛出schemes,打开app对应页面
        // for(var i=0; i < schemeAdr.length; i++){
        //   win.location.href = schemeAdr[i];
        // }
        win.location.href = schemeAdr;

        //由于部分ios中打开app后，WAP页面会被挂起，定时器不会被执行。因此可以做下优化：
        //WAP页重新被聚焦后，如果超过1s，认为APP被打开了，重新聚焦时就不必再跳转到APP下载页
        var startTime = Date.now();
        var delayTime = /safari/.test(UA) ? 3000 : 1000;
        var timer = setInterval(function () {
          var endTime = Date.now();
          if (endTime - startTime > delayTime) {
            clearInterval(timer);
            win.location.href = config.iosurl || config.url;
          }
        }, 25);
      } else {
        // 更改andriod兼容两张方式打开
        // for(var i=0; i < schemeAdr.length; i++){
        //   _this.creatIframe(schemeAdr[i]);
        // }
        _this.creatIframe(schemeAdr)
        //跳转下载页
        setTimeout(function () {
          win.location.href = config.url;
        }, 600);
      }
    },
    creatIframe: function (src) {
      // 如果用户已经安装了应用 抛出schemes,打开app对应页面
      var ifr = doc.createElement('iframe');
      ifr.style.display = 'none';
      ifr.src = src;
      doc.body.appendChild(ifr);
    },
    init: function (options) {
      var _this = this;
      if (!options) return; // 没有配置不执行
      if (_this.cookie('topBanner2')) return; // 用户关闭过topBanner直接返回
      this.config = this.getConfig(options);

      // 获得最终的创建DOM的数据
      _this.createDOM(this.config);
      _this.removeDOM();
      _this.domAnim(150);
      // _this.autoOpenScheme(); // 新增加自动打开app
    }
  };
  win[objName] = new Obj(); // 暴露的全局对象名称
})(window, document, 'topBanner');
topBanner.init({
  kanman: {
    ckey: 'CK1382108356199', // 看漫画微下载: CK1358783372301
    ios: 'id1243372811', // 看漫画:id1303470988
    appname: '看漫画', // 斗罗大陆IOS: id1303470988
    pkgname: 'com.wbxm.icartoon',
    scheme_Adr: 'tkanmanapp'
  },
  iyouman: {
    ckey: 'CK1383325844989', // 爱优漫微下载: CK1358783561013
    ios: 'id1243372811', // 斗罗大陆IOS:
    appname: '爱优漫',
    pkgname: 'com.comic.iyouman',
    scheme_Adr: 'tiyoumanapp'
  },
  shenmanhua: {
    ckey: 'CK1383322645544', // 神漫画微下载: CK1358783561013
    ios: 'id1243372811', // 斗罗大陆IOS:
    appname: '神漫画',
    pkgname: 'com.comic.shenmanhua',
    scheme_Adr: 'tshenmanhuaapp'
  },
  douluodalu3: {
    ckey: 'CK1358783561013', // 斗罗大陆微下载: CK1358783561013
    ios: 'id1243372811', // 斗罗大陆IOS:
    appname: '漫画台',
    pkgname: 'com.comic.manhuatai',
    scheme_Adr: 'tmanhuataiapp'
  },
  isaman: {
    ckey: 'CK1383328534446', // 爱飒漫画
    ios: 'id1243372811', //
    appname: '爱飒漫画',
    pkgname: 'com.comic.isaman',
    scheme_Adr: 'tmanhuataiapp'
  },
  manhuatai: {
    ckey: 'CK138297596322', // 漫画台微下载: CK1358783561013
    ios: 'id1243372811', // 漫画台IOS: id1243372811
    appname: '漫画台',
    pkgname: 'com.comic.manhuatai',
    scheme_Adr: 'tmanhuataiapp'
  },
  zymk: {
    ckey: 'CK1353950509167', // 知音漫客微下载: CK1353950509167
    ios: 'id1012491820', // 知音漫客IOS: id1012491820
    appname: '知音漫客',
    pkgname: 'cn.zymk.comic',
    scheme_Adr: 'zymkapp',
    feature: '<span class="jisu">人气漫画阅读且永久免费</span> <span class="noad">无广告</span>',
    style: '.topBannerBox{width:100%;z-index:9999;box-sizing:border-box;height:1.33333rem;}.topBannerBox .topBannerBD{position:fixed;top:0;width:100%;height:1.33333rem;background:rgba(0,0,0,0.75)}.topBannerBox .topBannerAnim{-webkit-transform:translateY(0);transform:translateY(0);transition:all 0.5s cubic-bezier(0.19, 1, 0.22, 1);z-index:9999}.topBannerBox .topBannerAnimHidden{-webkit-transform:translateY(-101%);transform:translateY(-101%);transition:all 0.3s cubic-bezier(0.55, 0.055, 0.675, 0.19);z-index:9999}.topBannerBox .close{position:absolute;top:0;right:0;width:.64rem;height:1.33333rem;line-height:1.33333rem;color:#999;padding:0;background:none;font-size:.64rem;font-family:SimSun, Tahoma, arial, SimHei, sans-serif;text-align:center;text-decoration:none;z-index:9999}.topBannerBox .txtBox,.topBannerBox .open{position:absolute;top:50%;-webkit-transform:translateY(-50%);transform:translateY(-50%)}.topBannerBox .open{right:.85333rem;display:inline-block;width:2.34667rem;line-height:.8rem;border-radius:.08rem;font-size:.32rem;text-decoration:none;color:#fff;text-align:center;white-space:nowrap;background:#ff424b}.topBannerBox .txtBox{left:.13333rem}.topBannerBox .txtBoxTxt{color:#fff;font-size:.32rem;margin:0 0 0 .21333rem}.topBannerBox .logoTxt{margin:0 0 .05333rem 0;color:#f9f978;font-size:.53333rem;line-height:1;font-weight:400}.topBannerBox .jisu{color:#a7f96f}.topBannerBox .noad{color:#ffcec5}.topBannerBox .cache{color:#9af5bf}</style>'
  }
});